package com.label.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.label.Dao.JiangXunJinMapper;
import com.label.Dao.LiZhiJxjMapper;
import com.label.Dao.PinKunStuMapper;
import com.label.Dao.ZhuXueJxjMapper;
import com.label.Model.Do.JiangXueJin;
import com.label.Model.Do.LiZhiJxj;
import com.label.Model.Do.PinKunStu;
import com.label.Model.Do.ZhuXueJxj;
import com.label.Service.DataImportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * @Author 75046
 * @Date 2022/5/17 11:00
 */

@Service
@Slf4j
public class DataImportServiceImpl implements DataImportService {

    @Resource
    private LiZhiJxjMapper liZhiJxjMapper;

    @Resource
    private ZhuXueJxjMapper zhuXueJxjMapper;

    @Resource
    private PinKunStuMapper pinKunStuMapper;

    @Resource
    private JiangXunJinMapper jiangXunJinMapper;

    private final static Map<String,String> title = new HashMap<String,String>(){
        {
            put("学号","userid");
            put("金额","money");
            put("类型","lzjLevel");
            put("起始学年","startYear");
            put("终止学年","endYear");
            put("奖学金等级","jxjLevel");
            put("困难等级","poorLevel");
        }
    };

    @Value("${file.path}")
    public String path;

    @Override
    public Boolean dataImport(String tableName,String fileName) throws Exception {
        List<Map<String,String>> data=excel(path+fileName);

        switch (tableName){
            case "lizhi":lizhi(data);break;
            case "jiangxuejin":jiangxuejin(data);break;
            case "pinkun":pinkun(data);break;
            case "zhuxuejin":zhuxuejin(data);break;
        }
        return true;
    }

    public void lizhi(List<Map<String,String>> data){
        if(data!=null&&!data.isEmpty()){
            for(Map<String,String> map:data){
                LiZhiJxj liZhiJxj=BeanUtil.copyProperties(map,LiZhiJxj.class);
                liZhiJxjMapper.insert(liZhiJxj);
            }
        }
    }

    public void jiangxuejin(List<Map<String,String>> data){
        if(data!=null&&!data.isEmpty()){
            for(Map<String,String> map:data){
                JiangXueJin jiangXueJin=BeanUtil.copyProperties(map,JiangXueJin.class);
                jiangXunJinMapper.insert(jiangXueJin);
            }
        }
    }

    public void zhuxuejin(List<Map<String,String>> data){
        if(data!=null&&!data.isEmpty()){
            for(Map<String,String> map:data){
                ZhuXueJxj zhuXueJxj=BeanUtil.copyProperties(map,ZhuXueJxj.class);
                zhuXueJxjMapper.insert(zhuXueJxj);
            }
        }
    }

    public void pinkun(List<Map<String,String>> data){
        if(data!=null&&!data.isEmpty()){
            for(Map<String,String> map:data){
                PinKunStu pinKunStu=BeanUtil.copyProperties(map,PinKunStu.class);
                pinKunStuMapper.insert(pinKunStu);
            }
        }
    }


    public static List<Map<String,String>> excel(String fileName) throws Exception {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new Exception("文件不存在!");
        }
        InputStream in = new FileInputStream(file);

        XSSFWorkbook sheets = new XSSFWorkbook(in);
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        XSSFRow titleRow = sheetAt.getRow(0);

        List<Map<String,String>> data=new LinkedList<>();
        // 循环获取每一行数据
        for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
            XSSFRow row = sheetAt.getRow(i);

            Map<String,String> rowData=new HashMap<>();
            for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
                XSSFCell titleCell = titleRow.getCell(index);
                XSSFCell cell = row.getCell(index);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                if (cell.getStringCellValue().equals("")) {
                    continue;
                }
                rowData.put(title.get(titleCell.getStringCellValue()),cell.getStringCellValue());
            }
            data.add(rowData);
        }

        return data;
    }


}
